package action;

import java.util.ArrayList;

import data.Event;
import data.EventDateTime;
import action.CheckIf;

import java.util.Calendar;

import parser.AddParser;

public class CheckFree extends Action {

	private static CheckIf checkIfObj = new CheckIf();
	private static String[] minutes = {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09",
		"10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
		"20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
		"30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
		"40", "41", "42", "43", "44", "45", "46", "47", "48", "49",
		"50", "51", "52", "53", "54", "55", "56", "57", "58", "59",
	};
	private static String[] hours = {"12", "1", "2", "3", "4", "5", "6", "7", "8", "9",
		"10", "11"};


	@Override
	public Event[] execute(String s) {
		if (!validateInput(s)) {
			return null;
		}
		String params = s.replace("checkfree ", "");
		Event checkFreeEvent = AddParser.parseEvent(params);
		if (checkFreeEvent != null) {
			return checkFree(checkFreeEvent);
		} else {
			return null;
		}

	}


	public static Event[] checkFree(Event checkFreeEvent) {

		int durationNeeded = checkFreeEvent.getDuration();
		int freeMinCount = 0;
		ArrayList<Event> freeSlots = new ArrayList<Event>();
		ArrayList<EventDateTime> recordOfMins = new ArrayList<EventDateTime>(); 
		//ArrayList<Event> listOfFreeSlots = new ArrayList<Event>();
		//boolean freeSlot = true;

		recordOfMins = trackingArray(checkFreeEvent);

		for(int i =0; i<recordOfMins.size();i++) {
			if(recordOfMins.get(i) == null) {
				freeMinCount = 0;
			} else {
				freeMinCount++;
				if(freeMinCount == durationNeeded) {
					Event freeSlotEvent = new Event();
					freeSlotEvent.setEnd(recordOfMins.get(i));
					freeSlotEvent.setStart(recordOfMins.get(i - freeMinCount + 1));
					freeSlots.add(freeSlotEvent);
					freeMinCount = 0;

				}

			}

		}
		
		
		Event[] arrayOfFreeSlots = freeSlots.toArray(new Event[freeSlots.size()]);

		
		
		
		
		//for debugging
		for(int x=0;x<arrayOfFreeSlots.length;x++)
			System.out.println(Integer.toString(arrayOfFreeSlots[x].getStart().get(Calendar.HOUR_OF_DAY)));

		return arrayOfFreeSlots;

	}

	public static boolean timeFlag(EventDateTime checkFreeTime) { 
		Event checkFreeEvent = new Event();
		checkFreeEvent.setStart(checkFreeTime);
		Event[] checkIfEvents = checkIfObj.checkIf(checkFreeEvent);
		if(checkIfEvents.length == 0) {
			return true;
		}

		else
			return false;
	}

	public static ArrayList<EventDateTime> trackingArray(Event checkFreeEvent) {
		ArrayList<EventDateTime> checkByMin = new ArrayList<EventDateTime>();
		int j = 0;

		for(String hr : hours) {
			for(String min : minutes) {
				checkFreeEvent = AddParser.parseEvent(checkFreeEvent.getStart().get(Calendar.DAY_OF_MONTH)+"/"+checkFreeEvent.getStart().get(Calendar.MONTH)+"/"+checkFreeEvent.getStart().get(Calendar.YEAR)+" "+hr+":"+min+"am");
				if(timeFlag(checkFreeEvent.getStart())) {
					checkByMin.add(j,checkFreeEvent.getStart());
				} else {
					checkByMin.add(j, null);
				}

				j++;
			}
		}

		for(String phr : hours) {
			for(String min : minutes) {
				checkFreeEvent = AddParser.parseEvent(checkFreeEvent.getStart().get(Calendar.DAY_OF_MONTH)+"/"+checkFreeEvent.getStart().get(Calendar.MONTH)+"/"+checkFreeEvent.getStart().get(Calendar.YEAR)+" "+phr+":"+min+"pm");
				if(timeFlag(checkFreeEvent.getStart())) {
					checkByMin.add(j, checkFreeEvent.getStart());
				} else {
					checkByMin.add(j, null);
				}
				j++;

			}	

		}

		return checkByMin;

	}


	@Override
	public Event[] undo() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public boolean isUndoable() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public String errorMessage() {
		// TODO Auto-generated method stub
		return null;
	}


	private boolean validateInput(String s) {
		// TODO Auto-generated method stub
		return true;
	}


	@Override
	public String getCommandName() {
		return "checkFree";
	}

}
